import { Component, OnInit } from '@angular/core';
import {NbDialogService, NbToastrService} from '@nebular/theme';
import {NbToastStatus} from '@nebular/theme/components/toastr/model';
import {LocalDataSource} from 'ng2-smart-table';
import {FoodService} from '../../service/food.service';

@Component({
  selector: 'app-company',
  templateUrl: './company.component.html',
  styleUrls: ['./company.component.css']
})
export class CompanyComponent implements OnInit {

  settings = {
    noDataMessage: '暂无数据',
    add: {
      addButtonContent: '<i class="nb-plus"></i>',
      createButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmCreate: true,
    },
    edit: {
      editButtonContent: '<i class="nb-edit"></i>',
      saveButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmSave: true,
    },
    delete: {
      deleteButtonContent: '<i class="nb-trash"></i>',
      confirmDelete: true,
    },
    columns: {
      id: {
        title: 'ID',
        type: 'number',
        editable: false,
      },
      Name: {
        title: '公司名称',
        type: 'string',
      },
      Account: {
        title: '账号',
        type: 'string',
      },
      Password: {
        title: '密码',
        type: 'string',
      },
      Tel: {
        title: '电话',
        type: 'string',
      },
      Address: {
        title: '地址',
        type: 'string',
      },
      Create_date: {
        title: '加入时间',
        type: 'string',
      },
    },
  };

  source: LocalDataSource = new LocalDataSource();
  public company: any = [];

  constructor(private foodService: FoodService,
              private dialogService: NbDialogService,
              private toastrService: NbToastrService,) {
    this.loadData();
  }

  async loadData() {
    const formData = new FormData();
    this.company = await this.foodService.getAll('?s=App.Fcpn.Selectall', formData);
    console.log(this.company);
    this.source.load(this.company.data.code);
  }

  onDeleteConfirm(event): void {
    if (window.confirm('确定删除?')) {
      const formData = new FormData();
      formData.append('id', event.data.id);
      this.foodService.post('?s=App.Fcpn.Dele' , formData).subscribe( data => {
        if (data.ret === 200) {
          this.toastrService.show('删除成功', '提示', {status: NbToastStatus.SUCCESS});
          event.confirm.resolve();
          this.loadData();
        } else {
          this.toastrService.show('删除失败', '提示', {status: NbToastStatus.DANGER});
          event.confirm.reject();
        }
      });
    } else {
      event.confirm.reject();
    }
  }

  onCreateConfirm(event): void {
    const formData = new FormData();
    if (event.newData.Name && event.newData.Account && event.newData.Address && event.newData.Password && event.newData.Tel) {
      formData.append('Account', event.newData.Account);
      formData.append('Name', event.newData.Name);
      formData.append('Address', event.newData.Address);
      formData.append('Password', event.newData.Password);
      formData.append('Tel', event.newData.Tel);
      formData.append('Create_date', this.transDate(new Date()));
      this.foodService.post('?s=App.Fcpn.Insert', formData).subscribe((data) => {
        if (data.ret === 200) {
          this.toastrService.show('创建成功', '提示', {status: NbToastStatus.SUCCESS});
          event.confirm.resolve();
          this.loadData();
        } else {
          this.toastrService.show('创建失败', '提示', {status: NbToastStatus.DANGER});
          event.confirm.reject();
        }
      });
    }
  }

  onEditConfirm(event): void {
    const formData = new FormData();
    if (event.newData.Name && event.newData.Account && event.newData.Address && event.newData.Password && event.newData.Tel) {
      formData.append('Account', event.newData.Account);
      formData.append('id', event.newData.id);
      formData.append('Name', event.newData.Name);
      formData.append('Address', event.newData.Address);
      formData.append('Password', event.newData.Password);
      formData.append('Tel', event.newData.Tel);
      this.foodService.post('?s=App.Fcpn.Update', formData).subscribe((data) => {
        if (data.ret === 200) {
          this.toastrService.show('创建成功', '提示', {status: NbToastStatus.SUCCESS});
          event.confirm.resolve();
          this.loadData();
        } else {
          this.toastrService.show('创建失败', '提示', {status: NbToastStatus.DANGER});
          event.confirm.reject();
        }
      });
    }
  }

  transDate(time) {
    time.setMinutes(time.getMinutes() - time.getTimezoneOffset());
    return time.toISOString().substring(0, 10);
  }

  ngOnInit() {
  }

}
